home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Science / Gnuplot 3.5 / docs / doc2ms.c < prev    next >
C/C++ Source or Header  |  1993-11-03  |  6KB  |  279 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: doc2ms.c%v 3.38.2.70 1993/02/08 02:19:29 woo Exp woo $";
  3. #endif
  4.  
  5.  
  6. /*
  7.  * doc2ms.c  -- program to convert Gnuplot .DOC format to *roff -ms document
  8.  * From hlp2ms by Thomas Williams 
  9.  *
  10.  * Modified by Russell Lang, 2nd October 1989
  11.  * to make vms help level 1 and 2 create the same ms section level.
  12.  *
  13.  * Modified to become doc2ms by David Kotz (David.Kotz@Dartmouth.edu) 12/89
  14.  * Added table and backquote support.
  15.  *
  16.  * usage:  doc2ms [file.doc [file.ms]]
  17.  *
  18.  *   where file.doc is a VMS .DOC file, and file.ms will be a [nt]roff
  19.  *     document suitable for printing with nroff -ms or troff -ms
  20.  *
  21.  * typical usage for GNUPLOT:
  22.  *
  23.  *   doc2ms gnuplot.doc | tbl | eqn | troff -ms
  24.  *
  25.  * or
  26.  *
  27.  *   doc2ms gnuplot.doc | groff -ms -et >gnuplot.ps
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <ctype.h>
  32.  
  33. #define MAX_NAME_LEN    256
  34. #define MAX_LINE_LEN    256
  35. #define LINE_SKIP        3
  36.  
  37. #define TRUE 1
  38. #define FALSE 0
  39.  
  40. typedef int boolean;
  41.  
  42. static boolean intable = FALSE;
  43.  
  44. main(argc,argv)
  45. int argc;
  46. char **argv;
  47. {
  48. FILE * infile;
  49. FILE * outfile;
  50.     infile = stdin;
  51.     outfile = stdout;
  52.     if (argc > 3) {
  53.         fprintf(stderr,"Usage: %s [infile [outfile]]\n", argv[0]);
  54.         exit(1);
  55.     }
  56.     if (argc >= 2) 
  57.         if ( (infile = fopen(argv[1],"r")) == (FILE *)NULL) {
  58.             fprintf(stderr,"%s: Can't open %s for reading\n",
  59.                 argv[0], argv[1]);
  60.             exit(1);
  61.         }
  62.     if (argc == 3)
  63.         if ( (outfile = fopen(argv[2],"w")) == (FILE *)NULL) {
  64.             fprintf(stderr,"%s: Can't open %s for writing\n",
  65.                 argv[0], argv[2]);
  66.         }
  67.     
  68.     init(outfile);
  69.     convert(infile,outfile);
  70.     finish(outfile);
  71.     exit(0);
  72. }
  73.  
  74.  
  75. init(b)
  76. FILE *b;
  77. {
  78.     /* in nroff, increase line length by 8 and don't adjust lines */
  79.     (void) fputs(".if n \\{.nr LL +8m\n.na \\}\n",b);
  80.     (void) fputs(".nr PO +0.3i\n",b);
  81.     (void) fputs(".so titlepag.ms\n",b);
  82.     (void) fputs(".pn 1\n",b);
  83.     (void) fputs(".bp\n",b);
  84.     (void) fputs(".ta 1.5i 3.0i 4.5i 6.0i 7.5i\n",b);
  85.     (void) fputs("\\&\n.sp 3\n.PP\n",b);
  86.     /* following line commented out by rjl
  87.       (void) fputs(".so intro\n",b);
  88.       */
  89. }
  90.  
  91.  
  92. convert(a,b)
  93.     FILE *a,*b;
  94. {
  95.     static char line[MAX_LINE_LEN];
  96.  
  97.     while (fgets(line,MAX_LINE_LEN,a)) {
  98.        process_line(line, b);
  99.     }
  100. }
  101.  
  102. process_line(line, b)
  103.     char *line;
  104.     FILE *b;
  105. {
  106.     switch(line[0]) {        /* control character */
  107.        case '?': {            /* interactive help entry */
  108.           break;            /* ignore */
  109.        }
  110.        case '@': {            /* start/end table */
  111.           if (intable) {
  112.              (void) fputs(".TE\n.KE\n", b);
  113.              (void) fputs(".EQ\ndelim off\n.EN\n\n",b);
  114.              intable = FALSE;
  115.           } else {
  116.              (void) fputs("\n.EQ\ndelim $$\n.EN\n",b);
  117.              (void) fputs(".KS\n.TS\ncenter box tab (@) ;\n", b);
  118.              (void) fputs("c c l .\n", b);
  119.              intable = TRUE;
  120.           }
  121.           /* ignore rest of line */
  122.           break;
  123.        }
  124.        case '#': {            /* latex table entry */
  125.           break;            /* ignore */
  126.        }
  127.        case '%': {            /* troff table entry */
  128.           if (intable)
  129.             (void) fputs(line+1, b); /* copy directly */
  130.           else
  131.             fprintf(stderr, "error: % line found outside of table\n");
  132.           break;
  133.        }
  134.        case '\n':            /* empty text line */
  135.        case ' ': {            /* normal text line */
  136.           if (intable)
  137.             break;        /* ignore while in table */
  138.           switch(line[1]) {
  139.              case ' ': {
  140.                 /* verbatim mode */
  141.                 fputs(".br\n",b); 
  142.                 putms_verb(line+1,b); 
  143.                 fputs(".br\n",b);
  144.                 break;
  145.              }
  146.              case '\'': {
  147.                 fputs("\\&",b);
  148.                 putms(line+1,b); 
  149.                 break;
  150.              }
  151.              default: {
  152.                 if (line[0] == '\n')
  153.                   putms(line,b); /* handle totally blank line */
  154.                 else
  155.                   putms(line+1,b);
  156.                 break;
  157.              }
  158.              break;
  159.           }
  160.           break;
  161.        }
  162.        default: {
  163.           if (isdigit(line[0])) { /* start of section */
  164.              if (!intable)    /* ignore while in table */
  165.                section(line, b);
  166.           } else
  167.             fprintf(stderr, "unknown control code '%c' in column 1\n", 
  168.                   line[0]);
  169.           break;
  170.        }
  171.     }
  172. }
  173.  
  174.  
  175. /* process a line with a digit control char */
  176. /* starts a new [sub]section */
  177.  
  178. section(line, b)
  179.     char *line;
  180.     FILE *b;
  181. {
  182.     static char string[MAX_LINE_LEN];
  183.     int sh_i;
  184.     static int old = 1;
  185.  
  186.   
  187.     (void) sscanf(line,"%d %[^\n]s",&sh_i,string);
  188.     
  189.     (void) fprintf(b,".sp %d\n",(sh_i == 1) ? LINE_SKIP : LINE_SKIP-1);
  190.     
  191.     if (sh_i > old) {
  192.        do
  193.         if (old!=1)    /* this line added by rjl */
  194.           (void) fputs(".RS\n.IP\n",b);
  195.        while (++old < sh_i);
  196.     }
  197.     else if (sh_i < old) {
  198.        do
  199.                if (sh_i!=1) /* this line added by rjl */
  200.                 (void) fputs(".RE\n.br\n",b);
  201.        while (--old > sh_i);
  202.     }
  203.     
  204.     /* added by dfk to capitalize section headers */
  205.     if (islower(string[0]))
  206.      string[0] = toupper(string[0]);
  207.     
  208.     /* next 3 lines added by rjl */
  209.     if (sh_i!=1) 
  210.      (void) fprintf(b,".NH %d\n%s\n.sp 1\n.LP\n",sh_i-1,string);
  211.     else 
  212.      (void) fprintf(b,".NH %d\n%s\n.sp 1\n.LP\n",sh_i,string);
  213.     old = sh_i;
  214.     
  215.     (void) fputs(".XS\n",b);
  216.     (void) fputs(string,b);
  217.     (void) fputs("\n.XE\n",b);
  218. }
  219.  
  220. putms(s, file)
  221.     char *s;
  222.     FILE *file;
  223. {
  224.     static boolean inquote = FALSE;
  225.  
  226.     while (*s != '\0') {
  227.        switch (*s) {
  228.           case '`': {        /* backquote -> boldface */
  229.              if (inquote) {
  230.                 fputs("\\fR", file);
  231.                 inquote = FALSE;
  232.              } else {
  233.                 fputs("\\fB", file);
  234.                 inquote = TRUE;
  235.              }
  236.              break;
  237.           }
  238.           case '\\': {        /* backslash */
  239.              fputs("\\\\", file);
  240.              break;
  241.           }
  242.           default: {
  243.              fputc(*s, file);
  244.              break;
  245.           }
  246.        }
  247.        s++;
  248.     }
  249. }
  250.  
  251. /*
  252.  * convert a verbatim line to troff input style, i.e. convert "\" to "\\"
  253.  * (added by Alexander Lehmann 01/30/93)
  254.  */
  255.  
  256. putms_verb(s, file)
  257.     char *s;
  258.     FILE *file;
  259. {
  260.     static boolean inquote = FALSE;
  261.  
  262.     while (*s != '\0') {
  263.        if (*s == '\\') {
  264.          fputc('\\', file);
  265.        }
  266.        fputc(*s, file);
  267.        s++;
  268.     }
  269. }
  270.  
  271. finish(b)        /* spit out table of contents */
  272. FILE *b;
  273. {
  274.     (void) fputs(".pn 1\n",b);
  275.     (void) fputs(".ds RH %\n",b);
  276.     (void) fputs(".af % i\n",b);
  277.     (void) fputs(".bp\n.PX\n",b);
  278. }
  279.